<?php
/**
 * Custom woocommerce currency symbol
 * @author Mr Rony<mrtrandangtrinh@gmail.com>
 */
add_filter('woocommerce_currencies', 'add_vnd_currency');

function add_vnd_currency($currencies) {
	$currencies['Đồng'] = __('Viet Nam Dong', 'woocommerce');
	return $currencies;
}

add_filter('woocommerce_currency_symbol', 'add_vnd_currency_symbol', 10, 2);

function add_vnd_currency_symbol($currency_symbol, $currency) {
	switch ($currency) {
		case 'Đồng': $currency_symbol = ' VNĐ';
			break;
	}
	return $currency_symbol;
}

/**
 * Hook for wp-admin logo
 * @author Mr Rony<mrtrandangtrinh@gmail.com>
 */
function woodgift_login_logo_url() {
	return get_bloginfo('url');
}

add_filter('login_headerurl', 'woodgift_login_logo_url');

function woodgift_login_logo_url_title() {
	return 'WoodGift';
}

add_filter('login_headertitle', 'woodgift_login_logo_url_title');

function woodgift_login_logo() {
	$options = get_option( 'mobileshop_theme_options' );
	?>
	<style type="text/css">
		body.login div#login h1 a {
			background-image: url(<?php echo $options['imglogo']; ?>);
			background-size: auto;
			height: 160px;
		}
	</style>
	<?php
}

add_action('login_enqueue_scripts', 'woodgift_login_logo');

/**
 * Register menu
 * @author Mr Rony<mrtrandangtrinh@gmail.com>
 */
register_nav_menus(array(
	'header' => 'Menu Header'
));

/**
 * Add image size
 * @author Mr Rony<mrtrandangtrinh@gmail.com>
 */
if (function_exists('add_image_size')) {
	add_image_size('thumb-products', 182, 146, false); //(no cropped)
	add_image_size('image-banner', 940, 324, false); //(no cropped)
	add_image_size('view-product', 277, 324, false); //(cropped)
}

/**
 * Register our sidebars and widgetized areas.
 * @author Mr Rony<mrtrandangtrinh@gmail.com>
 */
function woodgift_widgets_init() {
    
    register_sidebar( array(
        'name' => __( 'Shop by category', 'woodgift' ),
        'id' => 'sidebar-shop-category',
        'description' => __( 'Shop by category', 'html5blp' ),
        'before_widget' => '<div class="product-cat">',
        'after_widget' => "</div>",
        'before_title' => '<h1 class="title-left" style="display: none">',
        'after_title' => '</h1>',
    ) );
}
add_action( 'widgets_init', 'woodgift_widgets_init' );

/**
 * Remove Editor From Admin Appearance Menu
 * @author Mr Rony<mrtrandangtrinh@gmail.com>
 */
function woodgift_remove_the_editor() {
	remove_action('admin_menu', '_add_themes_utility_last', 101);
}

add_action('_admin_menu', 'woodgift_remove_the_editor', 1);


/**
 * Cart and checkout
 * @author Mr Rony<mrtrandangtrinh@gmail.com>
 */

if (!function_exists('woo_do_atomic')) {

	function woo_do_atomic($tag = '', $args = '') {

		if (!$tag) {
			return false;
		} // End IF Statement

		/* Do actions on the basic hook. */
		do_action($tag, $args);

		/* Loop through context array and fire actions on a contextual scale. */
		foreach ((array) woo_get_query_context() as $context) {

			do_action("{$tag}_{$context}", $args);
		} // End FOREACH Loop
	}

// End woo_do_atomic()
} // End IF Statement

function woo_nav_before() {
	woo_do_atomic('woo_nav_before');
}

// Add the cart link to the header
add_action('woo_nav_before', 'artificer_header_cart_link', 10);
if (!function_exists('artificer_header_cart_link')) {

	function artificer_header_cart_link() {
		if (class_exists('woocommerce')) {
			echo current(woocommerce_cart_link());
		}
	}

}

// Add the checkout link to the header
add_action('woo_nav_before', 'artificer_header_checkout_link', 20);
if (!function_exists('artificer_header_checkout_link')) {

	function artificer_header_checkout_link() {
		global $woocommerce;
	?>
			<a href="<?php echo $woocommerce->cart->get_checkout_url() ?>" class="checkout btn btn-success"><span class="lozenge"><i class="icon-shopping-cart icon-large"></i><?php _e('Thanh toán', 'woothemes') ?></span></a>
	<?php
	}

}


/* ------------------------------------------------------------------------------------------- */
/* AJAX FRAGMENTS */
/* ------------------------------------------------------------------------------------------- */

// Handle cart in header fragment for ajax add to cart
add_filter('add_to_cart_fragments', 'woocommerce_cart_link');

function woocommerce_cart_link() {
	global $woocommerce;

	ob_start();
	?>
		<a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count); ?> <?php _e('in your shopping cart', 'woothemes'); ?>" class="cart-button btn btn-info">
		<span class="label"><?php _e('Giỏ hàng', 'woothemes'); ?></span>
	<?php #echo $woocommerce->cart->get_cart_total();   ?>
		<span class="items badge"><?php echo sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count); ?></span>
		</a>
	<?php
	$fragments['a.cart-button'] = ob_get_clean();

	return $fragments;
}

if (!function_exists('woo_get_query_context')) {

	function woo_get_query_context() {
		global $wp_query, $query_context;

		/* If $query_context->context has been set, don't run through the conditionals again. Just return the variable. */
		if (isset($query_context->context) && is_array($query_context->context)) {

			return $query_context->context;
		} // End IF Statement

		$query_context = new stdClass();
		$query_context->context = array();

		/* Front page of the site. */
		if (is_front_page()) {

			$query_context->context[] = 'home';
		} // End IF Statement

		/* Blog page. */
		if (is_home() && !is_front_page()) {

			$query_context->context[] = 'blog';

			/* Singular views. */
		} elseif (is_singular()) {

			$query_context->context[] = 'singular';
			$query_context->context[] = "singular-{$wp_query->post->post_type}";

			/* Page Templates. */
			if (is_page_template()) {

				$to_skip = array('page', 'post');

				$page_template = basename(get_page_template());
				$page_template = str_replace('.php', '', $page_template);
				$page_template = str_replace('.', '-', $page_template);

				if ($page_template && !in_array($page_template, $to_skip)) {

					$query_context->context[] = $page_template;
				} // End IF Statement
			} // End IF Statement

			$query_context->context[] = "singular-{$wp_query->post->post_type}-{$wp_query->post->ID}";
		}

		/* Archive views. */ elseif (is_archive()) {
			$query_context->context[] = 'archive';

			/* Taxonomy archives. */
			if (is_tax() || is_category() || is_tag()) {
				$term = $wp_query->get_queried_object();
				$query_context->context[] = 'taxonomy';
				$query_context->context[] = $term->taxonomy;
				$query_context->context[] = "{$term->taxonomy}-" . sanitize_html_class($term->slug, $term->term_id);
			}

			/* User/author archives. */ elseif (is_author()) {
				$query_context->context[] = 'user';
				$query_context->context[] = 'user-' . sanitize_html_class(get_the_author_meta('user_nicename', get_query_var('author')), $wp_query->get_queried_object_id());
			}

			/* Time/Date archives. */ else {
				if (is_date()) {
					$query_context->context[] = 'date';
					if (is_year())
						$query_context->context[] = 'year';
					if (is_month())
						$query_context->context[] = 'month';
					if (get_query_var('w'))
						$query_context->context[] = 'week';
					if (is_day())
						$query_context->context[] = 'day';
				}
				if (is_time()) {
					$query_context->context[] = 'time';
					if (get_query_var('hour'))
						$query_context->context[] = 'hour';
					if (get_query_var('minute'))
						$query_context->context[] = 'minute';
				}
			}
		}

		/* Search results. */
		elseif (is_search()) {
			$query_context->context[] = 'search';

			/* Error 404 pages. */
		} elseif (is_404()) {
			$query_context->context[] = 'error-404';
		} // End IF Statement

		return $query_context->context;
	}

// End woo_get_query_context()
} // End IF Statement

/**
 * Remove Menu Page
 */
function agilsun_remove_menu_pages() {
	remove_menu_page('tools.php');
	remove_menu_page('plugins.php');
	remove_menu_page('edit.php');
	//remove_menu_page('edit-comments.php');
	remove_submenu_page( 'index.php', 'update-core.php' );
	remove_submenu_page( 'index.php', 'index.php' );
	//remove_submenu_page( 'themes.php', 'customize.php' );
}
add_action( 'admin_menu', 'agilsun_remove_menu_pages' );

/**
 * Disable Auto-Saving
 */
function agilsun_disable_autosave(){
    wp_deregister_script('autosave');
}
add_action( 'wp_print_scripts', 'agilsun_disable_autosave' );

/**
 * Disable Update
 */
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );

/**
 * Remove Editor From Admin Appearance Menu
 */
function agilsun_remove_the_editor() {
	remove_action('admin_menu', '_add_themes_utility_last', 101);
}
add_action('_admin_menu', 'agilsun_remove_the_editor', 1);

/**
 * Hide Help Tabs
 */
function agilsun_remove_help_tabs() {
    $screen = get_current_screen();
    $screen->remove_help_tabs();
}
add_action('admin_head', 'agilsun_remove_help_tabs');

/**
 * Remove menu tabs from the top toolbar
 */
function agilsun_config_admin_bar() {
	//call the global adminbar var
	global $wp_admin_bar;
	// hide the WP logo
	$wp_admin_bar->remove_menu('wp-logo');
	// hide link to the dashboard
	//$wp_admin_bar->remove_menu('dashboard');
	//hide links to WP
	//$wp_admin_bar->remove_menu('about');
	$wp_admin_bar->remove_menu('wporg');
	$wp_admin_bar->remove_menu('documentation');
	$wp_admin_bar->remove_menu('support-forums');
	$wp_admin_bar->remove_menu('feedback');
}
add_action('wp_before_admin_bar_render', 'agilsun_config_admin_bar');

include_once 'theme-options.php';